package com.linx.message.service.impl;

import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.linx.message.domain.bo.MessageInfoBo;
import com.linx.message.domain.vo.MessageInfoVo;
import com.linx.message.domain.MessageInfo;
import com.linx.message.mapper.MessageInfoMapper;
import com.linx.message.service.IMessageInfoService;

import java.util.List;
import java.util.Collection;

/**
 * 消息Service业务层处理
 *
 * @author linx
 * @date 2024-11-09
 */
@RequiredArgsConstructor
@Service
public class MessageInfoServiceImpl implements IMessageInfoService {

    private final MessageInfoMapper baseMapper;

    /**
     * 查询消息
     *
     * @param messageInfoId 主键
     * @return 消息
     */
    @Override
    public MessageInfoVo queryById(Long messageInfoId){
        return baseMapper.selectVoById(messageInfoId);
    }

    /**
     * 分页查询消息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 消息分页列表
     */
    @Override
    public TableDataInfo<MessageInfoVo> queryPageList(MessageInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MessageInfo> lqw = buildQueryWrapper(bo);
        Page<MessageInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的消息列表
     *
     * @param bo 查询条件
     * @return 消息列表
     */
    @Override
    public List<MessageInfoVo> queryList(MessageInfoBo bo) {
        LambdaQueryWrapper<MessageInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MessageInfo> buildQueryWrapper(MessageInfoBo bo) {
        LambdaQueryWrapper<MessageInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getIsDelete()), MessageInfo::getIsDelete, bo.getIsDelete());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageTitle()), MessageInfo::getMessageTitle, bo.getMessageTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageContent()), MessageInfo::getMessageContent, bo.getMessageContent());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageType()), MessageInfo::getMessageType, bo.getMessageType());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageSource()), MessageInfo::getMessageSource, bo.getMessageSource());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageTypeTwo()), MessageInfo::getMessageTypeTwo, bo.getMessageTypeTwo());
        lqw.eq(StringUtils.isNotBlank(bo.getIsSend()), MessageInfo::getIsSend, bo.getIsSend());
        lqw.eq(StringUtils.isNotBlank(bo.getIsRead()), MessageInfo::getIsRead, bo.getIsRead());
        return lqw;
    }

    /**
     * 新增消息
     *
     * @param bo 消息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MessageInfoBo bo) {
        MessageInfo add = MapstructUtils.convert(bo, MessageInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMessageInfoId(add.getMessageInfoId());
        }
        return flag;
    }

    /**
     * 修改消息
     *
     * @param bo 消息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MessageInfoBo bo) {
        MessageInfo update = MapstructUtils.convert(bo, MessageInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MessageInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除消息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
